package com.tanhua.server.controller;

import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.CommentsService;
import com.tanhua.server.service.MovementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author lifeihu
 * @date 2021/12/26
 * @TIME:18:50
 */
@RestController
@RequestMapping("/movements")
public class MovementController {
    @Autowired
    private MovementService movementService;
    @Autowired
    CommentsService commentsService;
    /**
     * 发布动态
     */
    @PostMapping
    public ResponseEntity movements(Movement movement, MultipartFile[] imageContent) throws IOException {
        movementService.publishMovement(movement,imageContent);
        return ResponseEntity.ok(null);
    }
    @GetMapping("/all")
    public ResponseEntity findByUserId(Long userId,
                                       @RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pr = movementService.findByUserId(userId,page,pagesize);
        return ResponseEntity.ok(pr);
    }
    /**
    * 查看好友动态
    */
    @GetMapping
    public ResponseEntity findFriendMovents(@RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pr=movementService.findFriendMovents(page,pagesize);
        return ResponseEntity.ok(pr);
    }
    /**
     * 查看推荐动态
     */
    @GetMapping("/recommend")
    public ResponseEntity findRecommendMovements(@RequestParam(defaultValue = "1") Integer page,
                                                 @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pr=movementService.findRecommendMovements(page,pagesize);
        return ResponseEntity.ok(pr);
    }
    @GetMapping("/{id}")
    public ResponseEntity findByMovementId(@PathVariable("id") String id){
        MovementsVo vo=movementService.findByMovementId(id);
        return ResponseEntity.ok(vo);
    }
    //评论点赞
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementId){
        Integer likeCount = commentsService.likeComment(movementId);
        return ResponseEntity.ok(likeCount);
    }
    /**
     * 评论取消点赞
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String movementId) {
        Integer likeCount = commentsService.dislikeComment(movementId);
        return ResponseEntity.ok(likeCount);
    }
    /**
     * 谁看过我
     */
    @GetMapping("/visitors")
    public ResponseEntity queryVisitorsList(){
        List<VisitorsVo> list = movementService.queryVisitorsList();
        return ResponseEntity.ok(list);
    }
}
